<?php
include 'sysco.function.php';

$con = mysql_connect($HOST, $USER, $PASS);
mysql_select_db($DB);

echo "Report from ".$_SESSION['from']." to ".$_SESSION['to'];
// ============================= MA visit time  ======================================//
// THIS MERCHANT IS OPEN FOR 11AM-10PM 
// lpLaVigaRwc = 11,22
// lp000051396857 = 6,15
// lp000051383792 = 11,21

$timeArr = array('11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22');

if ($MERCHANT == 'lpLaVigaRwc') {
  //$timeArr = range(11,23);
} else if ($MERCHANT == 'lp000051396857') {
  //$timeArr = range(6,16);
} else if ($MERCHANT == 'lp000051383792') {
  //$timeArr = range(11,22);
} else {
 // die('merchant not set with hours');  
}

$orderByTime = array();
$orderByTime2 = array();
$dataOrders = getOrdersByTime($MERCHANT);

// $menuSessions = getMenuSessionSlots($MERCHANT);
foreach ($dataOrders as $dataOrder) {
  $time = date('H', strtotime($dataOrder['CREATEDDATE']));
  $dateOfWeek = date('w', strtotime($dataOrder['CREATEDDATE']));
  
  $dateOfWeek = $dateOfWeek+1; // sunday is 1
  $startTime = '0';
  
  $orderByTime[$dateOfWeek][$time][] = $dataOrder['ID'];
  
}

$maxValue = 0;
$dateNo = 0;
foreach ($orderByTime as $k1 => $order) {
  foreach ($order as $k2 => $or1) {
    $orderByTime2[$k1][$k2] = count($or1);
    // $maxValue = ($maxValue < $orderByTime2[$k1][$k2]) ? $orderByTime2[$k1][$k2] : $maxValue;
    if ($maxValue < $orderByTime2[$k1][$k2]) {
      $maxValue = $orderByTime2[$k1][$k2];
      $dateNo = $k1;
    }
  }
}

$days = getDays();
foreach ($days as $dk => $day) {
  foreach ($timeArr as $time) {
    if (!isset($orderByTime2[$dk][$time])) {
      $orderByTime2[$dk][$time] = 0;
    }
  }
}
echo 'Max Value - '.$maxValue."\n";

$range = $maxValue/5;
if ($month == 1) {
  $arrNoDates = array(1 => 4, 2 => 4, 3 => 5, 4 => 5, 5 => 5, 6 => 4, 7 => 4 );  
} else if ($month == 2) {
  $arrNoDates = array(1 => 4, 2 => 4, 3 => 4, 4 => 4, 5 => 4, 6 => 4, 7 => 4 );
} 


$fp = fopen('reports/sysco_hours_'.$_SESSION['from'].'_'.$MERCHANT.'.csv', 'w');
$tmp = array('Day', 'Time From', 'Time To', 'Avg. Order Count', 'Key');
fputcsv($fp, $tmp);

// calc avg maxvalue
$avgMax = 0;
foreach ($orderByTime2 as $w => $orderByTime) {
  foreach ($orderByTime as $t => $orderCount) {
    $avgMax = ($avgMax < $orderCount/$arrNoDates[$w]) ? $orderCount/$arrNoDates[$w] : $avgMax;
  }
}
echo 'Max Avg Value - '.$avgMax."\n";

$avgMax = floor($avgMax/5)*5;
echo 'Rounded Max Avg Value - '.$avgMax."\n";

$rate = $avgMax/5;
echo 'Bin Value - '.$rate."\n";

foreach ($orderByTime2 as $w => $orderByTime) {
  foreach ($orderByTime as $t => $orderCount) {
    $lab = $t+1;
    $avg = $orderCount/$arrNoDates[$w];
    
    fputcsv($fp, array($days[$w], $t, $lab , $avg, getColor($avg, $rate)));
  }
}

function getColor($avgValue, $rate) {
  
  $v = ($avgValue == 0) ? 0 :($avgValue/$rate) - 0.0001;
  $v = min(5, floor($v));
  
  return $v; 
}

fputcsv($fp, array(" "));
fputcsv($fp, array("Legend", "--------"));
$legent = array();
for ($i = $rate; $avgMax >= $i; $i = ($i+$rate) ) {
  $legent[] = ($i - $rate).' - '.$i;
  $final = $i;
}
$legent[] = ($i-$rate).' + ';

fputcsv($fp, $legent);

fclose($fp);
?>